﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;
using Rafy;
using Rafy.ComponentModel;
using Rafy.Data;
using Rafy.Domain;
using Rafy.Domain.ORM;
using Rafy.Domain.ORM.Query;
using Rafy.Domain.Validation;
using Rafy.ManagedProperty;
using Rafy.MetaModel;
using Rafy.MetaModel.Attributes;

namespace Rafy.Book
{
    /// <summary>
    /// 自定义导航按钮
    /// </summary>
    [RootEntity]
    public partial class Base_Button : BookEntity
    {
        #region 引用属性
        //public static readonly Property<int> CatalogBookIdProperty = P<Base_Button>.Register(e => e.CatalogBookId);
        //public int CatalogBookId
        //{
        //    get { return this.GetProperty(CatalogBookIdProperty); }
        //    set { this.SetProperty(CatalogBookIdProperty, value); }
        //}
        //public static readonly RefEntityProperty<Base_CatalogBook> CatalogBookProperty =
        //    P<Base_Button>.RegisterRef(e => e.CatalogBook, CatalogBookIdProperty, ReferenceType.Parent);
        ///// <summary>
        ///// 所属目录
        ///// </summary>
        //public Base_CatalogBook CatalogBook
        //{
        //    get { return this.GetRefEntity(CatalogBookProperty); }
        //    set { this.SetRefEntity(CatalogBookProperty, value); }
        //}
        #endregion

        #region 组合子属性

        #endregion

        #region 一般属性
        public static readonly Property<int> F_BooksIdProperty = P<Base_Button>.Register(e => e.F_BooksId);
        /// <summary>
        /// 所属书籍【把按钮挂载到图书表下，减少配置麻烦】
        /// </summary>
        public int F_BooksId
        {
            get { return this.GetProperty(F_BooksIdProperty); }
            set { this.SetProperty(F_BooksIdProperty, value); }
        }
        

        public static readonly Property<string> F_BtnNameProperty = P<Base_Button>.Register(e => e.F_BtnName);
        /// <summary>
        /// 按钮名称
        /// </summary>
        public string F_BtnName
        {
            get { return this.GetProperty(F_BtnNameProperty); }
            set { this.SetProperty(F_BtnNameProperty, value); }
        }

        public static readonly Property<string> F_NavPageProperty = P<Base_Button>.Register(e => e.F_NavPage);
        /// <summary>
        /// 导航页面
        /// </summary>
        public string F_NavPage
        {
            get { return this.GetProperty(F_NavPageProperty); }
            set { this.SetProperty(F_NavPageProperty, value); }
        }

        public static readonly Property<string> F_PramsProperty = P<Base_Button>.Register(e => e.F_Prams);
        /// <summary>
        /// 这是参数标识，手工配置到数据表中，他与代码同时使用
        /// </summary>
        public string F_Prams
        {
            get { return this.GetProperty(F_PramsProperty); }
            set { this.SetProperty(F_PramsProperty, value); }
        }

        public static readonly Property<int> F_BtnIdProperty = P<Base_Button>.Register(e => e.F_BtnId);
        /// <summary>
        /// 所属公共按钮
        /// </summary>
        public int F_BtnId
        {
            get { return this.GetProperty(F_BtnIdProperty); }
            set { this.SetProperty(F_BtnIdProperty, value); }
        }
        #endregion

        #region 只读属性

        #endregion
    }

    /// <summary>
    /// 自定义导航按钮 列表类。
    /// </summary>
    public partial class Base_ButtonList : BookEntityList<Base_Button> { }

    /// <summary>
    /// 自定义导航按钮 仓库类。
    /// 负责 自定义导航按钮 类的查询、保存。
    /// </summary>
    public partial class Base_ButtonRepository : BookEntityRepository
    {
        /// <summary>
        /// 单例模式，外界不可以直接构造本对象。
        /// </summary>
        protected Base_ButtonRepository() { }

        [RepositoryQuery]
        public virtual Base_ButtonList GetsBy(int? bookId)
        {
            var q = this.CreateLinqQuery();
            q = q.Where(e => e.F_BooksId == bookId);
            return (Base_ButtonList)this.QueryData(q);
        }

    }

    /// <summary>
    /// 自定义导航按钮 配置类。
    /// 负责 自定义导航按钮 类的实体元数据的配置。
    /// </summary>
    internal class Base_ButtonConfig : BookEntityConfig<Base_Button>
    {
        /// <summary>
        /// 配置实体的元数据
        /// </summary>
        protected override void ConfigMeta()
        {
            //配置实体的所有属性都映射到数据表中。
            Meta.MapTable().MapAllProperties();
        }
    }
}